﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineLibrary.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/
        OnlineLibrary.Linq.OnlineLibraryDBDataContext role = new OnlineLibrary.Linq.OnlineLibraryDBDataContext();
        public ActionResult Index()
        {
            if (Boolean.Parse(Session["Admin"].ToString()) == true)
            {
                IEnumerable<OnlineLibrary.Linq.tblRole> list = role.tblRoles.ToList();
                return View(list);
            }
            else
            {
                return Redirect("Shared/Error");
            }
        }

        //
        // GET: /Role/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Role/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Role/Create

        [HttpPost]
        public ActionResult Create(OnlineLibrary.Linq.tblRole collection)
        {
            try
            {
                // TODO: Add insert logic here
                role.tblRoles.InsertOnSubmit(collection);
                role.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Role/Edit/5
 
        public ActionResult Edit(int id)
        {
            OnlineLibrary.Linq.tblRole RoleView = role.tblRoles.Single(p => p.RoleId == id);
            return View(RoleView);
            
        }

        //
        // POST: /Role/Edit/5

        [HttpPost]
        
        public ActionResult Edit(int id, OnlineLibrary.Linq.tblRole collection)
        {
            try
            {
                // TODO: Add update logic here
                if (Boolean.Parse(Session["Admin"].ToString()) == true)
                {
                    var list = role.tblRoles.SingleOrDefault(p => p.RoleId == id);
                    list.RoleName = collection.RoleName;
                    list.Description = collection.Description;

                    role.SubmitChanges();

                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect("Shared/Error");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Role/Delete/5
 
        public ActionResult Delete(int id)
        {
            OnlineLibrary.Linq.tblRole RoleView = role.tblRoles.Single(p => p.RoleId == id);
            var user_role = from p in role.tblUsers where p.Role == RoleView.RoleId select p;

            if (user_role.Count() != 0)
            {
                ViewData["Warning"] = "You cann't delete this role";
            }
            else {
                ViewData["Warning"] = "Are you sure you want to delete this role " + RoleView.RoleName.ToString() + "?";
            }
            return View(RoleView);
        }

        //
        // POST: /Role/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                if (Boolean.Parse(Session["Admin"].ToString()) == true)
                {
                    var list = role.tblRoles.Single(p => p.RoleId == id);
                    role.tblRoles.DeleteOnSubmit(list);
                    role.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect("Shared/Error");
                }

            }
            catch
            {
                return View();
            }
        }
    }
}
